package cn.kawa.pet.service.impl;

import cn.kawa.pet.domain.PetType;
import cn.kawa.pet.mapper.PetTypeMapper;
import cn.kawa.pet.service.IPetTypeService;
import cn.kawa.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> typeTree() {
        Map<Long, PetType> map = new HashMap<>();
        List<PetType> petTypes = petTypeMapper.loadAll();
        for (PetType petType : petTypes) {
            map.put(petType.getId(),petType);
        }

        List<PetType> typeTree=new ArrayList<>();
        for (PetType petType : petTypes) {
            if(petType.getPid()==null){
                typeTree.add(petType);
            }else{
                Long pid = petType.getPid();
                PetType parentType = map.get(pid);
                parentType.getChildren().add(petType);
            }
        }


        return typeTree;
    }
}
